package cn.itsource.sys.service.impl;

import cn.itsource.sys.domain.Role;
import cn.itsource.sys.mapper.RoleMapper;
import cn.itsource.sys.service.IRoleService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public void add(Role role) {
        super.add(role);
        if (role.getPermissions().size() > 0){
            roleMapper.addPermissions(role);
        }
        if (role.getMenus().size() > 0){
            roleMapper.addMenus(role);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        roleMapper.delPermissions(id);
        roleMapper.delMenus(id);
        //删除当前角色和员工中间表数据
        roleMapper.delEmpRoleByRoleId(id);
        super.del(id);
    }

    @Override
    @Transactional
    public void update(Role role) {
        super.update(role);
        roleMapper.delPermissions(role.getId());
        roleMapper.delMenus(role.getId());
        if (role.getPermissions().size() > 0){
            roleMapper.addPermissions(role);
        }
        if (role.getMenus().size() > 0){
            roleMapper.addMenus(role);
        }
    }

    @Override
    @Transactional
    public void patchDel(Long[] ids) {
        for (Long id : ids) {
            del(id);
        }
    }

}
